package com.starblink.search.category.ui.list.cell

import android.graphics.drawable.RotateDrawable
import androidx.core.content.ContextCompat
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.ext.color
import com.starblink.android.basic.ext.enableCorner
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.apollo.id2String
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.extension.extraOf
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.graphic.SimpleColorFilter
import com.starblink.basic.ext.load
import com.starblink.basic.ext.rotate0
import com.starblink.basic.ext.rotate180
import com.starblink.basic.route.RoutePage
import com.starblink.search.category.data.CategoryModel
import com.starblink.search.databinding.CellLevelOneBinding
import com.starblink.search.databinding.CellLevelThreeBinding
import com.starblink.search.databinding.CellLevelTwoBinding

class LevelOneCell(binding: CellLevelOneBinding, private val onClick: ((model: CategoryModel, position: Int) -> Unit)? = null) :
    BaseBindingVH<CategoryModel, CellLevelOneBinding>(binding) {

    init {
        itemView.enableCorner(16.dp2px())

    }

    override fun onBind(model: CategoryModel, position: Int) {
        binding.img.load(model.source.imageUrl?:"")
        binding.tvName.text = model.source.name
        itemView.setOnClickListener {
            onClick?.invoke(model, position)
        }

    }

}

class LevelTwoCell(binding: CellLevelTwoBinding, private val onClick: ((model: CategoryModel, position: Int) -> Unit)? = null) :
    BaseBindingVH<CategoryModel, CellLevelTwoBinding>(binding) {

    private val rightDrawable = RotateDrawable().apply {
        drawable = ContextCompat.getDrawable(itemView.context, com.starblink.basic.style.R.drawable.ic_svg_arrow_down_16_16).apply {
            colorFilter = SimpleColorFilter(color(com.starblink.basic.style.R.color.sk_red600))
        }
    }

    init {
        binding.tvName.setCompoundDrawablesWithIntrinsicBounds(null, null, rightDrawable, null)
    }

    override fun onBind(model: CategoryModel, position: Int) {
        binding.tvName.text = model.source.name
        binding.tvName.setOnClickListener {
            onClick?.invoke(model, position)
        }
        if (model.expand) rightDrawable.rotate180() else rightDrawable.rotate0()
    }

}

class LeveThreeCell(binding: CellLevelThreeBinding) :
    BaseBindingVH<CategoryModel, CellLevelThreeBinding>(binding) {

    override fun onBind(model: CategoryModel, position: Int) {
        binding.tvName.text = model.source.name
        binding.imgLogo.visibleOrGone(model.source.littleImageUrl?.isNotEmpty() == true) {
            load(model.source.littleImageUrl!!)
        }

        itemView.setOnClickListener {
            itemView.context.navigationTo(RoutePage.Search.PAGE_CATEGORY_GOODS) {
                extraOf(RoutePage.ARG.ID to (model.source.id2String))
                extraOf(RoutePage.ARG.TITLE to (model.source.name))
            }
        }
    }

}